require("plugin")

plugin.video = {
	new = function(owner)
		local self = { plugin = owner }
		setmetatable(self, { __index = plugin.video })
		return self
	end,
	
	activate = function(self)
		self.plugin.stoicheia:mountMedia(true)
		self:showPage(true)
	end,
	
	deactivate = function(self)
		self.plugin.stoicheia:mountMedia(false)
	end,
	
	filter = function(path, dir)
		if dir then return true end
		return common.suffixFilter(path, "avi", "mpg", "mpeg", "flv", "wmv", "m3u", "asx")
	end,
	
	showPage = function(self, reload)
		if reload then
			self.plugin.stoicheia:showPage("video", function(event, f)
				if event ~= "html" then return end
				local dir = string.format("%s/video", common.getConfig("path.user.datadir"))
				local tree = common.tree(dir, true)
				local w, h = self.plugin.stoicheia:getScreenSize()
				f:beginList(100, 150, w - 400, h - 250)
				common.traverse(tree, function(node, name, depth, leaf, path, leafcount, childcount)
					if name == nil then return end
					if childcount == 0 then return end
					local notify
					if leaf then
						notify = path
						name = name:gsub("%.[^.]+$", "")
					end
					f:renderListItem(name, notify, (depth - 1) * 2)
				end, dir, self.filter)
				f:endList()
			end)
		end
		self.plugin.stoicheia:setControls(true, "back")
	end,
	
	controlEvent = function(self, event)
		if event == "back" then
			self.plugin.stoicheia:showStartPage()
			return
		end
		if event == "pause" then
			if not self:checkPlaying() then return end
			self.plugin.stoicheia:setControls(false, "stop", "continue", "backward", "forward")
			self:togglePause()
			return
		end
		if event == "continue" then
			if not self:checkPlaying() then return end
			self.plugin.stoicheia:setControls(false, "stop", "pause", "backward", "forward")
			self:togglePause()
			return
		end
		if event == "stop" then
			if not self:checkPlaying() then return end
			self:stop()
			self.plugin.stoicheia:showPagewin(true)
			self:showPage(false)
			return
		end
		if event == "forward" then
			if not self:checkPlaying() then return end
			self:seek(60)
			return
		end
		if event == "backward" then
			if not self:checkPlaying() then return end
			self:seek(-60)
			return
		end
		log("plugin.video", "Unknown video control `%s'", event)
	end,
	
	pageEvent = function(self, event, arg)
		assert(event == "CLICKED")
		self.plugin.stoicheia:showPagewin(false)
		self.plugin.stoicheia:setControls(false, "stop", "pause", "backward", "forward")
		self:play(arg)
	end,
	
	isPlaylist = function(self, path)
		return path:endsWith("asx") or path:endsWith("m3u")
	end,
	
	play = function(self, path)
		local options = common.getConfig("plugin.video.options")
		local playlist
		if self:isPlaylist(path) then playlist = "-playlist" end
		self.plugin.stoicheia:clear("#000000")
		self.task = task.executew(
			"mplayer",
			"-fs",
			"-rootwin",
			"-slave",
			"-quiet",
			options[path],
			playlist,
			path
		)
	end,
	
	isPlaying = function(self)
		return #(util.pgrep("mplayer")) > 0
	end,
	
	checkPlaying = function(self)
		if self:isPlaying() then return true end
		self.task = nil
		self.plugin.stoicheia:showPagewin(true)
		self:showPage(false)
		return false
	end,
	
	togglePause = function(self)
		assert(self.task)
		self.task:sendln("pause")
	end,
	
	stop = function(self)
		assert(self.task)
		self.task:sendln("quit")
		self.task:close()
		self.task = nil
	end,
	
	seek = function(self, seconds)
		assert(self.task)
		self.task:sendln(string.format("seek %d", seconds))
	end,
	
	check = function(self)
		self.plugin.stoicheia:checkCmd(256, "mplayer", "--help")
	end
}


